<?php

namespace App\Http\Controllers\Setting;

use Validator;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Platform;
use App\Models\UserPlatform;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PlatformController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */


    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    public function index(Request $request)
    {
        if ($request->ajax()) {

            $rows = $request->get("rows");

            $platform = $this->filters($request, new Platform());

            $paginator = $platform->orderBy($request->get("sidx"), $request->get("sord"))->paginate($rows);

            $rows = [];
            foreach ($paginator as $page) {
                $rows[] = $page;
            }

            $data = [
                'rows' => $rows,
                'total'=> $paginator->lastPage(),
                'page' => $paginator->currentPage(),
                'records' => $paginator->count(),
            ];

            return response()->json($data);

        } else {
            return view('setting/platform/index');
        }
        
    }

    public function edit(Request $request) {

        $this->validate($request, [
            'id' => 'required',
        ]);

        $oper = $request->get('oper');

        if ($oper == "del") {
            $platform = Platform::find($request->get('id'));
            $platform->delete();
            return response()->json(["success" => true]);
        }

        $platform = new Platform();
        if ($oper == "edit") {
    
            $platform = Platform::find($request->get('id'));
            if (!$platform) {
                return response()->json(["success" => false]);
            }
        }

        if ($oper == "add") {
        
            $platform->id = $request->get('id');
        }

        $platform->name = $request->get('name');
        $platform->save();

        return response()->json(["success" => true]);
    }


    public function get(int $id)
    {
        $UP = UserPlatform::where("user_id", $id)->get();

        $data = [];
        foreach ($UP as $up) {
            $data[] = $up->platform_id;
        }
        return response()->json($data);
    }

    public function user(Request $request) {

        if ($request->isMethod('post')) {

            $this->validate($request, [
                'id' => 'required',
            ]);

            $id = (int)$request->get('id');
            $ids = $request->get('ids', []);
            DB::transaction(function () use ($id, $ids) {

                UserPlatform::where('user_id', $id)->delete();
                foreach ($ids as $platform_id) {
                    $UserPlatform = new UserPlatform();
                    $UserPlatform->user_id = $id;
                    $UserPlatform->platform_id = $platform_id;
                    $UserPlatform->save();
                }
            });
        }

        return redirect()->back()->withInput(['配置成功']);
    }
}
